package com.senmo.data;

import com.senmo.model.ChannelListResult;
import com.senmo.model.ChannelModel;
import com.senmo.model.DeviceListResult;
import com.senmo.model.DeviceModel;
import com.senmo.request.DataHull;
import com.senmo.request.RequestFactory;

import java.util.ArrayList;
import java.util.HashMap;

import lx.af.manager.GlobalThreadManager;

/**
 * author: lx
 * date: 16-1-5
 */
public class DeviceDataProvider {

    private static DeviceDataProvider sInstance = new DeviceDataProvider();

    private final Object mLock = new Object();
    private ArrayList<DeviceModel> mDeviceList;
    private HashMap<String, DeviceModel> mDeviceMap = new HashMap<>();

    public static DeviceDataProvider getInstance() {
        return sInstance;
    }

    public DeviceModel getDevice(String device_id) {
        return mDeviceMap.get(device_id);
    }

    public void getDeviceList(final boolean refresh, final DeviceListCallback c) {
        GlobalThreadManager.runInThreadPool(new Runnable() {
            @Override
            public void run() {
                getDeviceList(refresh);
                if (c != null) {
                    GlobalThreadManager.runInUiThread(new Runnable() {
                        @Override
                        public void run() {
                            c.onGetDeviceList(mDeviceList);
                        }
                    });
                }
            }
        });
    }

    public void getDeviceChannelList(final String device_id, final boolean refresh, final DeviceChannelCallback c) {
        GlobalThreadManager.runInThreadPool(new Runnable() {
            @Override
            public void run() {
                final ArrayList<ChannelModel> list = getDeviceChannelList(device_id, refresh);
                if (c != null) {
                    GlobalThreadManager.runInUiThread(new Runnable() {
                        @Override
                        public void run() {
                            c.onGetDeviceChannels(list);
                        }
                    });
                }
            }
        });
    }

    public void clear() {
        synchronized (mLock) {
            mDeviceList.clear();
            mDeviceMap.clear();
        }
    }

    public ArrayList<DeviceModel> getDeviceList(boolean refresh) {
        synchronized (mLock) {
            if (!refresh && mDeviceList != null && mDeviceList.size() != 0) {
                return mDeviceList;
            }
            DataHull d = RequestFactory.getDeviceList().request();
            if (d.isRequestSuccess()) {
                DeviceListResult result = d.getParsedData();
                mDeviceList = result.devices;
                if (mDeviceList != null && mDeviceList.size() != 0) {
                    for (DeviceModel device : mDeviceList) {
                        mDeviceMap.put(device.device_id, device);
                    }
                }
            }
            return mDeviceList;
        }
    }

    public ArrayList<ChannelModel> getDeviceChannelList(String device_id, boolean refresh) {
        synchronized (mLock) {
            DeviceModel device = mDeviceMap.get(device_id);
            if (device == null) {
                // no such device, just return null
                return null;
            }
            if (!refresh && device.channels != null && device.channels.size() != 0) {
                return device.channels;
            }
            DataHull d = RequestFactory.getChannelList(device_id).request();
            if (d.isRequestSuccess()) {
                ChannelListResult result = d.getParsedData();
                device.channels = result.device_channels;
            }
            return device.channels;
        }
    }

    public interface DeviceListCallback {
        void onGetDeviceList(ArrayList<DeviceModel> list);
    }

    public interface DeviceChannelCallback {
        void onGetDeviceChannels(ArrayList<ChannelModel> list);
    }

}
